#!/usr/bin/python
#
#

import sys

PYTHON_VERSION = sys.version_info


import telnetlib

class extremeTelnet(telnetlib.Telnet):
    def __init__(self, host=None, port=0, user=None, password=None,
                    nopager=True, timeout=30):
        if PYTHON_VERSION < (2, 6):
            telnetlib.Telnet.__init__(self, host, port)
        else:
            telnetlib.Telnet.__init__(self, host, port, timeout)

        self.vendor = 'extreme'
        self.__enable = None
        self.command = None
        if not user and not password:
            self.__user = None
            self.__password = None
        elif not user:
            self.__user = None
            self.__password = password
        elif not password:
            self.__user = user
            self.__password = None
        else:
            self.__user = user
            self.__password = password
            self.Login(nopager=nopager)

    def setUser(self, user):
        self.__user = user

    def setPassword(self, password):
        self.__password = password

    def setEnable(self, enable):
        self.__enable = enable

    def Login(self, nopager=True):
        if self.__user and self.__password:
            self.read_until("login: ")
            self.write('%s\n' % self.__user)
            self.read_until("password: ")
            self.write('%s\n' % self.__password)
            self.read_until("# ")

            if nopager:
                self.Execute('disable clipaging')
        else:
            raise ValueError

    def Enable(self, enable=None):
        return

    def setCommand(self, command):
        self.__command = command

    def Execute(self, command):
        self.write('%s\n' % command)
        return self.expect([ '\n.* # $', ])


if __name__ == '__main__':

    import getpass

    addrs = [
                # ...
            ]

    for addr in addrs:
        user = raw_input('Enter user name for %s: ' %addr)  
        password= getpass.getpass('Enter password: ')

        print 'Getting startup config from %s...' % addr,

        et = extremeTelnet(addr, user=user, password=password)
        # et.Enable(enable)

        config = et.Execute('show configuration')[2]

        open('config.%s' % addr, 'w').write(config)
        print ' done.'

